```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring MVC注解详解 | @RequestBody与@ResponseBody</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            margin-top: 1.5em;
            margin-bottom: 0.8em;
            color: #222;
        }
        .hero-overlay {
            background-color: rgba(0, 0, 0, 0.4);
        }
        .code-block {
            background-color: #f6f8fa;
            border-radius: 6px;
            padding: 16px;
            overflow-x: auto;
        }
        .code-keyword {
            color: #d63384;
        }
        .code-class {
            color: #6f42c1;
        }
        .code-string {
            color: #22863a;
        }
        .code-comment {
            color: #6a737d;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(255, 223, 0, 0.3);
            z-index: -1;
            transition: all 0.3s ease;
        }
        .highlight:hover::after {
            height: 50%;
            background-color: rgba(255, 223, 0, 0.4);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative h-96 flex items-center justify-center bg-cover bg-center" style="background-image: url('https://images.unsplash.com/photo-1555066931-4365d14bab8c?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80')">
        <div class="hero-overlay absolute inset-0"></div>
        <div class="container mx-auto px-6 relative z-10 text-center">
            <h1 class="text-4xl md:text-5xl font-bold text-white mb-4">Spring MVC注解解析</h1>
            <p class="text-xl md:text-2xl text-white mb-8">深入理解@RequestBody与@ResponseBody</p>
            <div class="inline-block bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 px-6 rounded-lg transition duration-300">
                <i class="fas fa-book mr-2"></i>开始学习
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-4xl">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-blue-600 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">引言</h2>
            </div>
            <p class="text-lg text-gray-700 mb-6">现代的微服务架构中，前后端通过HTTP进行的请求和响应交互变得更加普遍。在这一过程中，数据的格式化传输成为了至关重要的一环。如今，JSON已经成为了一种主流的数据格式，不仅简单易读，还能够很好地与Java对象进行映射。</p>
            
            <div class="my-8">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1725855548205-726010b1-7ef8-4c08-9cea-2ec6eed02855.png" alt="HTTP请求响应示意图" class="rounded-lg shadow-md w-full">
            </div>
            
            <p class="text-lg text-gray-700">在Spring MVC中，我们如何轻松处理JSON数据呢？这就离不开两个关键的注解：<span class="highlight font-semibold">@RequestBody</span> 和 <span class="highlight font-semibold">@ResponseBody</span>。它们是我们开发过程中进行数据转换的强大工具。<span class="highlight">@RequestBody</span> 允许我们将前端传递的JSON请求体自动转换为Java对象，而<span class="highlight">@ResponseBody</span>则能将我们的Java对象转换为JSON格式的响应体，发送回前端。这样的转换机制极大地简化了数据处理流程，让我们不必手动解析或构建JSON。</p>
        </section>

        <!-- SpringMVC Model Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-blue-600 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">SpringMVC的请求与响应模型</h2>
            </div>
            <p class="text-lg text-gray-700 mb-6">在Spring MVC中，请求和响应模型是核心的概念，它为Web应用提供了一种简洁且强大的方式来处理HTTP请求与响应。这个模型是基于"前端控制器"模式（Front Controller Pattern）设计的，具体由<code class="bg-gray-100 px-2 py-1 rounded">DispatcherServlet</code>作为前端控制器，协调处理用户的HTTP请求，并生成适当的HTTP响应。</p>
            
            <div class="my-8">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1725855586271-e0261671-c628-4d53-8d80-1004bdc83f82.png" alt="Spring MVC请求响应模型" class="rounded-lg shadow-md w-full">
            </div>

            <!-- Request Process -->
            <div class="mb-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-arrow-right mr-2 text-blue-600"></i>请求的处理流程
                </h3>
                <p class="text-gray-700 mb-4">Spring MVC中，一个请求的处理过程可以分为多个步骤，主要涉及<code class="bg-gray-100 px-2 py-1 rounded">DispatcherServlet</code>、处理器映射（Handler Mapping）、处理器（Handler）、处理器适配器（Handler Adapter）、视图解析器（View Resolver）等组件。</p>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">1. DispatcherServlet作为入口</h4>
                        <p class="text-gray-700"><code class="bg-gray-100 px-1 py-0.5 rounded">DispatcherServlet</code> 是Spring MVC的核心，它是一个Servlet，用于捕获所有的HTTP请求。它根据配置映射的URL，将请求路由到合适的处理器（Controller）。这个过程中的核心组件是处理器映射，它基于映射规则来确定哪个处理器来处理这个请求。</p>
                    </div>
                    
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">2. Handler处理器与Controller</h4>
                        <p class="text-gray-700">处理器（<code class="bg-gray-100 px-1 py-0.5 rounded">Handler</code>）通常是一个控制器（<code class="bg-gray-100 px-1 py-0.5 rounded">@Controller</code>类），用于处理具体的业务逻辑。<code class="bg-gray-100 px-1 py-0.5 rounded">DispatcherServlet</code>在找到相应的处理器后，会通过处理器适配器来调用处理器方法。处理器方法的输入通常是HTTP请求的参数，而输出可以是视图（View）或数据对象。</p>
                    </div>
                    
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">3. HandlerAdapter与多种请求的支持</h4>
                        <p class="text-gray-700">不同类型的请求处理器可能存在差异，<code class="bg-gray-100 px-1 py-0.5 rounded">HandlerAdapter</code>提供了一种机制来适配处理器的调用，例如可以处理标准控制器方法、REST控制器或异步请求等。它的灵活性体现在能够支持多种处理器实现，允许扩展。</p>
                    </div>
                    
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">4. 返回结果的处理</h4>
                        <p class="text-gray-700">处理器方法执行完业务逻辑后，会返回一个<code class="bg-gray-100 px-1 py-0.5 rounded">ModelAndView</code>对象，包含视图名和模型数据。在Spring MVC中，模型是数据部分，而视图是用来展示数据的部分。视图解析器根据<code class="bg-gray-100 px-1 py-0.5 rounded">ModelAndView</code>中的视图名找到对应的视图文件，通常是JSP、Thymeleaf等模版引擎生成的页面。</p>
                    </div>
                </div>
            </div>

            <!-- Response Process -->
            <div class="mb-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-reply mr-2 text-blue-600"></i>响应的生成流程
                </h3>
                <p class="text-gray-700 mb-4">Spring MVC的响应机制同样是围绕<code class="bg-gray-100 px-2 py-1 rounded">DispatcherServlet</code>展开的，它会根据处理器返回的结果，结合视图解析器生成最终的HTTP响应。</p>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">1. 视图解析与渲染</h4>
                        <p class="text-gray-700">当处理器方法返回<code class="bg-gray-100 px-1 py-0.5 rounded">ModelAndView</code>时，Spring MVC会调用视图解析器（<code class="bg-gray-100 px-1 py-0.5 rounded">ViewResolver</code>），将逻辑视图名解析为实际的视图实现，例如JSP、HTML页面或其他模版引擎生成的页面。模型数据会传递给视图引擎，用于动态渲染内容。</p>
                    </div>
                    
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">2. 数据响应（JSON/XML）</h4>
                        <p class="text-gray-700">如果控制器方法使用了<code class="bg-gray-100 px-1 py-0.5 rounded">@ResponseBody</code>注解，处理器会直接返回Java对象而不是视图名。Spring MVC会通过消息转换器（<code class="bg-gray-100 px-1 py-0.5 rounded">HttpMessageConverter</code>）将Java对象序列化为JSON或XML格式，返回给客户端。这对于REST API的开发尤其重要，通过这样的方式，Spring MVC能够支持轻量级的、无状态的响应数据。</p>
                    </div>
                    
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">3. 异常处理与响应</h4>
                        <p class="text-gray-700">Spring MVC通过全局异常处理机制（如<code class="bg-gray-100 px-1 py-0.5 rounded">@ControllerAdvice</code>和<code class="bg-gray-100 px-1 py-0.5 rounded">@ExceptionHandler</code>）来统一处理控制器方法中的异常。异常处理器可以返回一个合适的视图或数据格式的响应，使得客户端能够获得有意义的错误信息。</p>
                    </div>
                </div>
            </div>

            <!-- Model Features -->
            <div class="bg-blue-50 p-6 rounded-lg border border-blue-200">
                <h4 class="font-semibold text-lg mb-3 text-blue-800">请求与响应模型的特点</h4>
                <ul class="list-disc pl-5 text-gray-700 space-y-2">
                    <li><span class="font-semibold">灵活性</span>：通过不同的Handler Mapping和Handler Adapter，Spring MVC能够灵活支持多种类型的请求和响应处理方式。</li>
                    <li><span class="font-semibold">模块化</span>：视图、数据模型、控制器、异常处理、消息转换等环节都是解耦的，可以根据需求单独配置和优化。</li>
                    <li><span class="font-semibold">可扩展性</span>：开发者可以通过自定义的Handler、View Resolver、Message Converter等机制来扩展Spring MVC的功能。</li>
                </ul>
            </div>
        </section>

        <!-- @RequestBody Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-blue-600 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">@RequestBody注解</h2>
            </div>
            <p class="text-lg text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">@RequestBody</code> 注解是Spring MVC中非常重要的一个功能，它允许将HTTP请求体直接绑定到控制器方法的参数上，通常用于处理JSON、XML或其他格式的请求数据。这一注解极大地简化了开发RESTful API时对请求体内容的处理，特别是复杂对象的解析与转换。</p>
            
            <div class="my-8">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1725855698359-e712e629-2490-477f-b594-7bf1925339d7.png" alt="@RequestBody工作流程" class="rounded-lg shadow-md w-full">
            </div>

            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">基本用法</h3>
                    <div class="code-block mb-6">
                        <pre><code class="text-sm"><span class="code-keyword">@PostMapping</span>(<span class="code-string">"/createUser"</span>)
<span class="code-keyword">public</span> ResponseEntity&lt;User&gt; createUser(<span class="code-keyword">@RequestBody</span> User user) {
    <span class="code-comment">// 这里的user对象直接从请求体中解析</span>
    <span class="code-keyword">return</span> <span class="code-keyword">new</span> ResponseEntity&lt;&gt;(user, HttpStatus.CREATED);
}</code></pre>
                    </div>
                    <p class="text-gray-700">在这个例子中，Spring会自动将客户端发送的JSON数据（比如<code class="bg-gray-100 px-1 py-0.5 rounded">{ "name": "John", "age": 30 }</code>）反序列化为<code class="bg-gray-100 px-1 py-0.5 rounded">User</code>类的对象，并传递给<code class="bg-gray-100 px-1 py-0.5 rounded">createUser</code>方法。开发者无需手动解析和转换JSON数据。</p>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">工作原理</h3>
                    <ol class="list-decimal pl-5 text-gray-700 space-y-2">
                        <li><strong>解析请求体</strong>：Spring会从HTTP请求的body中读取数据。</li>
                        <li><strong>选择消息转换器</strong>：基于<code class="bg-gray-100 px-1 py-0.5 rounded">Content-Type</code>头部信息，Spring MVC会自动选择合适的<code class="bg-gray-100 px-1 py-0.5 rounded">HttpMessageConverter</code>。</li>
                        <li><strong>转换为Java对象</strong>：消息转换器会根据参数类型，将请求体中的数据转换为指定的Java对象。</li>
                    </ol>
                    
                    <div class="mt-4 bg-gray-50 p-4 rounded-lg">
                        <h4 class="font-semibold text-gray-700 mb-2">内置消息转换器：</h4>
                        <ul class="list-disc pl-5 text-gray-700">
                            <li><strong>JSON</strong>：<code class="bg-gray-100 px-1 py-0.5 rounded">MappingJackson2HttpMessageConverter</code></li>
                            <li><strong>XML</strong>：<code class="bg-gray-100 px-1 py-0.5 rounded">Jaxb2RootElementHttpMessageConverter</code></li>
                            <li><strong>String类型</strong>：<code class="bg-gray-100 px-1 py-0.5 rounded">StringHttpMessageConverter</code></li>
                        </ul>
                    </div>
                </div>
            </div>

            <div class="mt-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4">使用场景与示例</h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">处理JSON请求</h4>
                        <div class="code-block mb-3">
                            <pre><code class="text-xs"><span class="code-keyword">@PostMapping</span>(<span class="code-string">"/api/users"</span>)
<span class="code-keyword">public</span> ResponseEntity&lt;User&gt; addUser(<span class="code-keyword">@RequestBody</span> User user) {
    <span class="code-comment">// user对象包含请求体中的JSON数据</span>
    <span class="code-keyword">return</span> <span class="code-keyword">new</span> ResponseEntity&lt;&gt;(user, HttpStatus.CREATED);
}</code></pre>
                        </div>
                        <p class="text-sm text-gray-700">客户端请求体的内容如下：</p>
                        <div class="code-block mt-2">
                            <pre><code class="text-xs">{
  <span class="code-string">"username"</span>: <span class="code-string">"Alice"</span>,
  <span class="code-string">"age"</span>: <span class="code-string">25</span>
}</code></pre>
                        </div>
                    </div>
                    
                    <div class="bg-white p-6 rounded-lg shadow-md card-hover">
                        <h4 class="font-semibold text-lg mb-3 text-blue-700">处理复杂对象</h4>
                        <div class="code-block mb-3">
                            <pre><code class="text-xs"><span class="code-keyword">public</span> <span class="code-keyword">class</span> <span class="code-class">User</span> {
    <span class="code-keyword">private</span> String username;
    <span class="code-keyword">private</span> Profile profile;
    <span class="code-keyword">private</span> List&lt;String&gt; roles;
    <span class="code-comment">// getters and setters</span>
}</code></pre>
                        </div>
                        <p class="text-sm text-gray-700">可以处理如下嵌套JSON结构：</p>
                        <div class="code-block mt-2">
                            <pre><code class="text-xs">{
  <span class="code-string">"username"</span>: <span class="code-string">"John"</span>,
  <span class="code-string">"profile"</span>: {
    <span class="code-string">"address"</span>: <span class="code-string">"123 Street"</span>,
    <span class="code-string">"phone"</span>: <span class="code-string">"555-1234"</span>
  },
  <span class="code-string">"roles"</span>: [<span class="code-string">"admin"</span>, <span class="code-string">"user"</span>]
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- @ResponseBody Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-blue-600 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">@ResponseBody注解</h2>
            </div>
            <p class="text-lg text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">@ResponseBody</code> 注解是 Spring MVC 中用于直接将控制器方法的返回值作为 HTTP 响应体返回给客户端的注解。与 <code class="bg-gray-100 px-2 py-1 rounded">@RequestBody</code> 处理请求体类似，<code class="bg-gray-100 px-2 py-1 rounded">@ResponseBody</code> 负责将控制器方法的返回结果序列化为指定的格式（例如 JSON 或 XML），并写入 HTTP 响应体中。</p>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">基本用法</h3>
                    <div class="code-block mb-6">
                        <pre><code class="text-sm"><span class="code-keyword">@GetMapping</span>(<span class="code-string">"/getUser"</span>)
<span class="code-keyword">@ResponseBody</span>
<span class="code-keyword">public</span> User getUser() {
    User user = <span class="code-keyword">new</span> User(<span class="code-string">"Alice"</span>, 25);
    <span class="code-keyword">return</span> user;
}</code></pre>
                    </div>
                    <p class="text-gray-700">在这个例子中，控制器返回了一个 <code class="bg-gray-100 px-1 py-0.5 rounded">User</code> 对象。Spring MVC 会将这个 <code class="bg-gray-100 px-1 py-0.5 rounded">User</code> 对象通过 <code class="bg-gray-100 px-1 py-0.5 rounded">HttpMessageConverter</code> 序列化为 JSON，并将其写入 HTTP 响应体中。</p>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">@RestController</h3>
                    <p class="text-gray-700 mb-4">如果整个控制器的所有方法都需要返回 JSON 或其他格式的响应，可以使用 <code class="bg-gray-100 px-2 py-1 rounded">@RestController</code> 注解，它相当于为控制器类的所有方法自动添加了 <code class="bg-gray-100 px-2 py-1 rounded">@ResponseBody</code> 注解。</p>
                    <div class="code-block">
                        <pre><code class="text-sm"><span class="code-keyword">@RestController</span>
<span class="code-keyword">@RequestMapping</span>(<span class="code-string">"/api"</span>)
<span class="code-keyword">public</span> <span class="code-keyword">class</span> <span class="code-class">UserController</span> {

    <span class="code-keyword">@GetMapping</span>(<span class="code-string">"/user"</span>)
    <span class="code-keyword">public</span> User getUser() {
        <span class="code-keyword">return</span> <span class="code-keyword">new</span> User(<span class="code-string">"Alice"</span>, 25);
    }

    <span class="code-keyword">@GetMapping</span>(<span class="code-string">"/status"</span>)
    <span class="code-keyword">public</span> String getStatus() {
        <span class="code-keyword">return</span> <span class="code-string">"OK"</span>;
    }
}</code></pre>
                    </div>
                </div>
            </div>
            
            <div class="mt-8 bg-blue-50 p-6 rounded-lg border border-blue-200">
                <h4 class="font-semibold text-lg mb-3 text-blue-800">Content-Type 和 Accept 头</h4>
                <p class="text-gray-700 mb-4">在使用 <code class="bg-gray-100 px-1 py-0.5 rounded">@ResponseBody</code> 时，Spring MVC 会根据请求的 <code class="bg-gray-100 px-1 py-0.5 rounded">Accept</code> 头来选择合适的消息转换器。也可以通过 <code class="bg-gray-100 px-1 py-0.5 rounded">produces</code> 属性手动指定响应格式：</p>
                <div class="code-block">
                    <pre><code class="text-sm"><span class="code-keyword">@GetMapping</span>(value = <span class="code-string">"/getUser"</span>, produces = <span class="code-string">"application/json"</span>)
<span class="code-keyword">@ResponseBody</span>
<span class="code-keyword">public</span> User getUser() {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span> User(<span class="code-string">"Alice"</span>, 25);
}</code></pre>
                </div>
            </div>
        </section>

        <!-- JSON Processing Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-blue-600 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">JSON的处理</h2>
            </div>
            <p class="text-lg text-gray-700 mb-6">在 Spring MVC 中，处理 JSON 格式的数据是开发 RESTful API 的关键环节。Spring MVC 通过 <code class="bg-gray-100 px-2 py-1 rounded">@RequestBody</code> 和 <code class="bg-gray-100 px-2 py-1 rounded">@ResponseBody</code> 注解，结合内置的 <code class="bg-gray-100 px-2 py-1 rounded">HttpMessageConverter</code> 机制，使得 JSON 数据的请求和响应变得简单高效。</p>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">处理JSON请求</h3>
                    <div class="code-block mb-4">
                        <pre><code class="text-sm"><span class="code-keyword">@PostMapping</span>(<span class="code-string">"/user"</span>)
<span class="code-keyword">public</span> ResponseEntity&lt;String&gt; createUser(<span class="code-keyword">@RequestBody</span> User user) {
    <span class="code-comment">// user对象会自动解析请求体中的JSON数据</span>
    <span class="code-keyword">return</span> ResponseEntity.ok(<span class="code-string">"User created successfully"</span>);
}</code></pre>
                    </div>
                    <p class="text-gray-700">客户端发送的 POST 请求体中包含 JSON 数据，Spring 会通过 Jackson 库将 JSON 转换为 <code class="bg-gray-100 px-1 py-0.5 rounded">User</code> 对象。</p>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">处理JSON响应</h3>
                    <div class="code-block mb-4">
                        <pre><code class="text-sm"><span class="code-keyword">@GetMapping</span>(<span class="code-string">"/user/{id}"</span>)
<span class="code-keyword">@ResponseBody</span>
<span class="code-keyword">public</span> User getUser(<span class="code-keyword">@PathVariable</span> Long id) {
    <span class="code-keyword">return</span> userService.findUserById(id);
}</code></pre>
                    </div>
                    <p class="text-gray-700">方法返回一个 <code class="bg-gray-100 px-1 py-0.5 rounded">User</code> 对象，Spring MVC 会将其序列化为 JSON 响应。</p>
                </div>
            </div>
            
            <div class="mt-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4">自定义JSON解析</h3>
                <p class="text-gray-700 mb-4">可以通过配置 <code class="bg-gray-100 px-2 py-1 rounded">ObjectMapper</code> 来自定义 Jackson 的行为：</p>
                <div class="code-block">
                    <pre><code class="text-sm"><span class="code-keyword">@Configuration</span>
<span class="code-keyword">public</span> <span class="code-keyword">class</span> <span class="code-class">JacksonConfig</span> {
    <span class="code-keyword">@Bean</span>
    <span class="code-keyword">public</span> Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        <span class="code-keyword">return</span> builder -> builder
            .serializationInclusion(JsonInclude.Include.NON_NULL)
            .dateFormat(<span class="code-keyword">new</span> SimpleDateFormat(<span class="code-string">"yyyy-MM-dd"</span>));
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Error Handling Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-blue-600 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">错误处理与验证</h2>
            </div>
            <p class="text-lg text-gray-700 mb-6">在 Spring MVC 中，处理请求参数时的错误处理与数据验证是开发稳定和可靠应用程序的重要组成部分，尤其是在处理来自外部请求的数据时。通过合理的错误处理和数据验证机制，我们可以确保系统的健壮性，并且向用户提供明确的错误反馈。</p>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">验证机制</h3>
                    <div class="code-block mb-4">
                        <pre><code class="text-sm"><span class="code-keyword">public</span> <span class="code-keyword">class</span> <span class="code-class">UserDTO</span> {
    <span class="code-keyword">@NotNull</span>(message = <span class="code-string">"Username cannot be null"</span>)
    <span class="code-keyword">@Size</span>(min = 3, max = 15, message = <span class="code-string">"Username must be between 3 and 15 characters"</span>)
    <span class="code-keyword">private</span> String username;

    <span class="code-keyword">@NotNull</span>(message = <span class="code-string">"Email cannot be null"</span>)
    <span class="code-keyword">@Email</span>(message = <span class="code-string">"Email should be valid"</span>)
    <span class="code-keyword">private</span> String email;
    <span class="code-comment">// Getters and setters</span>
}</code></pre>
                    </div>
                    <div class="code-block">
                        <pre><code class="text-sm"><span class="code-keyword">@PostMapping</span>(<span class="code-string">"/register"</span>)
<span class="code-keyword">public</span> ResponseEntity&lt;String&gt; registerUser(<span class="code-keyword">@Valid</span> <span class="code-keyword">@RequestBody</span> UserDTO user) {
    <span class="code-comment">// Handle successful registration</span>
}</code></pre>
                    </div>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">全局异常处理</h3>
                    <div class="code-block">
                        <pre><code class="text-sm"><span class="code-keyword">@ControllerAdvice</span>
<span class="code-keyword">public</span> <span class="code-keyword">class</span> <span class="code-class">GlobalExceptionHandler</span> {

    <span class="code-keyword">@ExceptionHandler</span>(MethodArgumentNotValidException.<span class="code-keyword">class</span>)
    <span class="code-keyword">public</span> ResponseEntity&lt;Map&lt;String, String&gt;&gt; handleValidationExceptions(
            MethodArgumentNotValidException ex) {
        Map&lt;String, String&gt; errors = <span class="code-keyword">new</span> HashMap&lt;&gt;();
        ex.getBindingResult().getAllErrors().forEach(error -> {
            String fieldName = ((FieldError) error).getField();
            String errorMessage = error.getDefaultMessage();
            errors.put(fieldName, errorMessage);
        });
        <span class="code-keyword">return</span> <span class="code-keyword">new</span> ResponseEntity&lt;&gt;(errors, HttpStatus.BAD_REQUEST);
    }
}</code></pre>
                    </div>
                </div>
            </div>
            
            <div class="mt-8 bg-yellow-50 p-6 rounded-lg border border-yellow-200">
                <h4 class="font-semibold text-lg mb-3 text-yellow-800">错误处理最佳实践</h4>
                <ul class="list-disc pl-5 text-gray-700 space-y-2">
                    <li><strong>精细化异常捕获</strong>：不同的异常类型应该有不同的处理方式。</li>
                    <li><strong>一致的错误格式</strong>：为前端提供结构化的错误响应格式。</li>
                    <li><strong>日志记录与监控</strong>：对于严重的系统异常，应记录日志以便排查。</li>
                </ul>
            </div>
        </section>

        <!-- Mermaid Diagram -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-blue-600 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">注解工作流程图示</h2>
            </div>
            <div class="mermaid">
                graph TD
                    A[客户端请求] -->|HTTP Request with JSON| B[DispatcherServlet]
                    B --> C[Handler Mapping]
                    C --> D[Controller]
                    D -->|@RequestBody| E[HttpMessageConverter]
                    E --> F[Java Object]
                    F --> D
                    D -->|@ResponseBody| G[HttpMessageConverter]
                    G --> H[JSON Response]
                    H --> I[HTTP Response]
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto px-6 text-center">
            <div class="mb-4">
                <p class="text-lg font-semibold">技术小馆</p>
            </div>
            <div>
                <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-white transition duration-300">
                    <i class="fas fa-globe mr-2"></i>http://www.yuque.com/jtostring
                </a>
            </div>
            <div class="mt-6 text-gray-400 text-sm">
                &copy; 2024 技术小馆. All rights reserved.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```